#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const process = require('process');
const paths = require('./paths.js');
const readFile = require('./readFile.js');
const createFile = require('./createFile.js');
require('shelljs/global');
require('colors');
function mergeValue (arr1, arr2) {
    let tempObj = {};
    let keys = [];
    arr1.forEach((item, index) => {
        !item && (tempObj[item] = true);
    });
    arr2.forEach((key, idx) => {
        if (!tempObj[key]) {
            tempObj[key] = true;
        }
    });
    for (const key in tempObj) {
        keys.push(key);
    }
    return keys;
}
module.exports = function createIgnore () {
    return new Promise ((resolve, reject) => {
        const prjIgnorePath = path.resolve(paths.prjRootPath, '.gitignore');
        const defIgnorePath = path.resolve(paths.modPath, '.gitignoreConfig');
        let defIgnoreArry = [];
        let prjIgnoreArry = [];
        if (!test('-f', prjIgnorePath)) {
            cp('-rf', defIgnorePath, prjIgnorePath);
        } else {
            readFile(defIgnorePath).then((defIgnore) => {
                defIgnoreArry = defIgnore.split(/\r?\n/ig);
                readFile(prjIgnorePath).then((prjIgnore) => {
                    prjIgnoreArry = prjIgnore.split(/\r?\n/ig);
                    const data = mergeValue(defIgnoreArry, prjIgnoreArry);
                    createFile(data.join('\n'), prjIgnorePath);
                    resolve();
                });
            });
        };
    });
}
